package org.aeon.client.view.components.menuPanel;

import org.aeon.client.context.GlobalContext;
import org.aeon.entity.User;
import org.aeon.utils.SwingUtil;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

/**
 * @author: MissingAeon
 * @date: 2024/2/22 19:03
 * @description:
 */
public class PersonalInfoDialog extends JPanel {

    private final JLabel nicknameLabel;
    private final JLabel usernameLabel;
    private final JLabel stateLabel;
    private final JLabel emailLabel;
    private final JButton button;

    public PersonalInfoDialog(Component comp) {
        SwingUtil.setSize(this, new Dimension(270, 350));
        // 设置位置
        this.setBounds(comp.getWidth() - 5, 5, 270, 350);

        final Border border = BorderFactory.createMatteBorder(1, 1, 1, 1, Color.LIGHT_GRAY);
        this.setBackground(new Color(0xEEE6E2));
        this.setBorder(border);
        this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

        this.add(Box.createVerticalStrut(40));

        // 昵称
        nicknameLabel = new JLabel("昵称");
        nicknameLabel.setFont(new Font("黑体", Font.BOLD, 18));
        nicknameLabel.setAlignmentX(Component.RIGHT_ALIGNMENT);
        this.add(nicknameLabel);

        this.add(Box.createVerticalStrut(20));

        // 账号
        usernameLabel = new JLabel("用户名");
        usernameLabel.setFont(new Font("黑体", Font.BOLD, 12));
        usernameLabel.setAlignmentX(Component.RIGHT_ALIGNMENT);
        this.add(usernameLabel);

        this.add(Box.createVerticalStrut(20));

        // 在线
        stateLabel = new JLabel("在线");
        stateLabel.setFont(new Font("黑体", Font.BOLD, 12));
        stateLabel.setAlignmentX(Component.RIGHT_ALIGNMENT);
        this.add(stateLabel);

        this.add(Box.createVerticalStrut(60));

        // 邮箱
        emailLabel = new JLabel("邮箱");
        emailLabel.setFont(new Font("黑体", Font.BOLD, 12));
        emailLabel.setAlignmentX(Component.RIGHT_ALIGNMENT);
        this.add(emailLabel);

        this.add(Box.createVerticalStrut(90));

        // 修改资料按钮
        button = new JButton("修改资料");
        // button.setFont(new Font("黑体", Font.BOLD, 24));
        button.setAlignmentX(Component.LEFT_ALIGNMENT);
        this.add(button);

        this.addFocusListener(new FocusAdapter() {
            @Override
            public void focusLost(FocusEvent e) {
                GlobalContext.getContext().getMainUI().removePopUpLayer(PersonalInfoDialog.this);
            }
        });

        load();
    }

    private void load() {
        final User user = GlobalContext.getContext().getCurrentUser();
        if (user != null) {
            nicknameLabel.setText(user.getNickname());
            usernameLabel.setText(user.getUsername());
            emailLabel.setText(user.getEmail());
        }
    }
}
